#!/usr/bin/env python

import urllib2
import zipfile
import os
import shutil
import findertools
import sys
import tempfile
from StringIO import StringIO

# Check to see if the latestrev currently exists, if not we're going to write one
homedir = os.path.expanduser('~') # Gets the users home directory, which we will store .chrupdate and any files related to the script that need to be used in the future, such as the current revision
scriptDir = '%s/.chrupdate/' % homedir
revTxtPath = '%slatestrev.txt' % scriptDir
if (os.path.exists(scriptDir) != True):
	print 'Current revision not found, creating new revision file'
	try:
		os.system('mkdir %s' % scriptDir)
		os.system('touch %s' % revTxtPath)
	except IOError:
		pass

print 'Getting current revision...'
curRevFile = open(revTxtPath) #reads the last revision downloaded with ChromiumUpdater
curRev = curRevFile.read()
curRevFile.close()
print 'Current Revision: %i' % int(curRev)

print 'Getting latest revision...'
response = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/LATEST')
latestRev = response.read()
print 'Latest Revision: %i' % (int(latestRev))

# Check to see if we even need to update
if (int(curRev) == int(latestRev)):
	print 'Your Chromium is updated to the latest revision: %i' % int(curRev)
	sys.exit(0)

#workingDir = '/Users/slehan/Desktop/ChromiumUpdate/'
workingDir = '/tmp/ChromiumUpdate/'
chromiumZipPath = '%s%i-update.zip' % (workingDir, (int(latestRev)))
chromiumAppPath = 'chrome-mac/' #the path of the chromium executable within the zip file
chromiumAppExtracted = '%schromium-48560/' % (workingDir) # path of the extracted executable
pathToUnzippedApp = '%schrome-mac/Chromium.app' % (chromiumAppExtracted)

# Make a folder in /tmp that is called ChromiumUpdate
print 'Making directory /tmp/ChromiumUpdate/'
os.mkdir(workingDir)

# we have the revision, now we need to download the zip and extract it
print 'Downloading the latest revision from snapshots...'
latestZip = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/%i/chrome-mac.zip' % (int(latestRev)), '%s' % chromiumZipPath)

output = open(chromiumZipPath, 'w') #delete any current file there
output.write(latestZip.read())
output.close()

# we have the .zip now we need to extract the Chromium.app file, it's in chrome-mac/Chromium.app/
# We will use os.system() to execute unzip on the file
print 'Unzipping Chromium zip file'
command = 'unzip %s -d %s' % (chromiumZipPath, chromiumAppExtracted)
os.system(command)

#clear console
print '\n' * 1000

# Move the .app from the newly unzipped folder into /Applications
# First we need to remove the current .app from Applications
print 'Removing Chromium.app from Applications'
print 'Moving Chromium.app from it''s unzipped location to Applications'

shutil.rmtree('/Applications/Chromium.app')
# Now we need to move it from pathToUnzippedApp to /Applications
shutil.move(pathToUnzippedApp, '/Applications/Chromium.app')

print 'Launching Chromium'
os.system('open /Applications/Chromium.app')

print 'Enjoy Revision %i! Please report all bugs to http://code.google.com/p/chromium/issues/entry\nCleaning /tmp while you browse' % (int(latestRev))

# Save the current revision to .chrupdate
latestRevFile = open(revTxtPath, 'w')
latestRevFile.write(latestRev)

shutil.rmtree(workingDir)
sys.exit(0)
